package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.listener.IEditTextChangeListener;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.utils.RegexUtils;
import com.hisan.freeride.common.utils.WorksSizeCheckUtil;
import com.hisan.freeride.common.view.ListPopup;
import com.hisan.freeride.databinding.RideUserlistBinding;
import com.hisan.freeride.home.model.RideUser;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;


/**
 * 创建时间 : 2017/6/12
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：添加常用联系人页面
 */
public class RideUserListActivity extends BaseActivity<RideUserlistBinding>{
    private int id;
    private String name="添加";
    //人数选择弹窗
    private ListPopup mListPopup;
    private ListPopup.Builder builder;
    private String[] names=new String[]{"身份证","护照","军人证","港澳通行证","驾驶证","港澳回乡证或台胞证","临时身份证"
            ,"外国人永久居留身份证","其他"};
    private List<String> namelist=new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.ride_userlist;
    }

    @Override
    protected void initView() {
        mBinding.add.setEnabled(false);
        builder=new ListPopup.Builder(this);
        //1.创建工具类对象 把要改变颜色的Button先传过去
        WorksSizeCheckUtil.textChangeListener textChangeListener = new WorksSizeCheckUtil.textChangeListener(mBinding.add);
        //2.把所有要监听的edittext都添加进去
        textChangeListener.addAllEditText(mBinding.username,mBinding.phone,mBinding.document);
        //3.接口回调 在这里拿到boolean变量 根据isHasContent的值决定 tv 应该设置什么颜色
        WorksSizeCheckUtil.setChangeListener(new IEditTextChangeListener() {
            @Override
            public void textChange(boolean isHasContent) {
                if(isHasContent){
                    mBinding.add.setEnabled(true);
                }else{
                    mBinding.add.setEnabled(false);
                }
            }
            @Override
            public void afterChange(Editable editable) {
            }
        });
        for (String name:names){
            builder.addItem(name);
            namelist.add(name);
            mListPopup=builder.build();
            mListPopup.getHome_titel().setText("请选择证件类型");
        }
        Bundle bundle= getIntent().getExtras();
        if (!CollectionUtils.isNullOrEmpty(bundle)){
            name="更新";
            id=bundle.getInt("id");
            getdata();
        }else {
            name="添加";
        }
    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
        mBinding.selectTypesof.setOnClickListener(v -> {

        });
        mBinding.add.setOnClickListener(v -> {
            String username= mBinding.username.getText().toString();
            String typesof=mBinding.typesof.getText().toString();
            String document=mBinding.document.getText().toString();
            String phone=mBinding.phone.getText().toString();
            if (!CollectionUtils.isNullOrEmpty(username)&&!CollectionUtils.isNullOrEmpty(typesof)
                    &&!CollectionUtils.isNullOrEmpty(document)&&!CollectionUtils.isNullOrEmpty(phone)){
                if (RegexUtils.isTel(phone)){
                    showToast("请输入正确的手机号.....");
                    return;
                }else {
                    if (name.equals("添加"))
                        addUser(username,typesof,document,phone);
                    else updata(username,typesof,document,phone);
                }

            }else {
                showToast("请输入完整信息.....");
                return;
            }
        });
        mBinding.selectTypesof.setOnClickListener(v -> {
            mListPopup.showPopupWindow();
            mListPopup.setOnListPopupItemClickListener(position -> {
                if (!CollectionUtils.isNullOrEmpty(namelist)) {
                    mBinding.typesof.setText(namelist.get(position));
                }
                if (!CollectionUtils.isNullOrEmpty(mListPopup)) mListPopup.dismiss();
            });
        });
    }
    /**
     * 更新乘坐人
     * @param username
     * @param typesof
     * @param document
     * @param phone
     */
    private void updata(String username, String typesof, String document, String phone) {
        HttpParams params=new HttpParams();
        params.put("name",username);
        params.put("card_type",typesof);
        params.put("card_number",document);
        params.put("mobile",phone);
        params.put("id",id);
        OkGo.put(AppConfig.pdata)
                .tag(this)
                .params(params)
                .execute(new DialogCallback<Object>(RideUserListActivity.this){
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (isOk(response)){
                            showToast("更新成功");
                            Intent intent=new Intent();
                            setResult(99, intent);
                            finishActivity(true);
                        }else {
                            showError(response);
                        }
                    }
                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });

    }

    /**
     * 添加常用乘坐人
     * @param username
     * @param typesof
     * @param document
     * @param phone
     */
    private void addUser(String username, String typesof, String document, String phone) {
        HttpParams params=new HttpParams();
        params.put("name",username);
        params.put("card_type",typesof);
        params.put("card_number",document);
        params.put("mobile",phone);
        OkGo.post(AppConfig.pcreate)
                .tag(this)
                .params(params)
                .execute(new DialogCallback<Object>(RideUserListActivity.this){
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Object> response) {
                        super.onSuccess(response);
                        if (isOk(response)){
                            showToast("添加成功");
                            Intent intent=new Intent();
                            setResult(99, intent);
                            finishActivity(true);

                        }else {
                            showError(response);
                        }
                    }
                    @Override
                    public void onError(com.lzy.okgo.model.Response<Object> response) {
                        super.onError(response);
                        showError(response);
                    }
                });
    }
    private void getdata(){
        OkGo.<List<RideUser>>get(AppConfig.dpcreate)
                .tag(this)
                .params("id",id)
                .execute(new DialogCallback<List<RideUser>>(this){
                    @Override
                    public void onSuccess(Response<List<RideUser>> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                                RideUser rideUser= response.body().get(0);
                                mBinding.username.setText(rideUser.getName());
                                mBinding.typesof.setText(rideUser.getCard_type());
                                mBinding.document.setText(rideUser.getCard_number());
                                mBinding.phone.setText(rideUser.getMobile());
                        }
                    }

                });
    }
    @Override
    protected void loadData(boolean isRefresh) {}
}
